<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 {{ total }} 条记录</span>
          <el-button
            @click="open('add')"
            icon="el-icon-plus"
            size="small"
            type="primary"
            round>
            添加面经
          </el-button>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%">
          <el-table-column
            prop="content"
            label="标题"
          >
          </el-table-column>
          <el-table-column
            prop="creator"
            label="作者"
          >
          </el-table-column>
          <el-table-column
            prop="likeCount"
            label="点赞"
          >
          </el-table-column>
          <el-table-column
            prop="views"
            label="浏览数">
          </el-table-column>
          <el-table-column
            prop="createdAt"
            label="更新时间"
          >
          </el-table-column>
          <el-table-column label="更新时间" width="120px">
             <template #default="{ row }">
              <div class="actions">
                <i @click="open('preview', row.id)" class="el-icon-view"></i>
                <i @click="open('edit', row.id)" class="el-icon-edit-outline"></i>
                <i @click="del(row.id)" class="el-icon-delete"></i>
              </div>
             </template>
          </el-table-column>
        </el-table>
      </template>
      <el-pagination
        background
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="total">
      </el-pagination>
    </el-card>
    <el-drawer
      size="50%"
      :title="title"
      :visible.sync="isShow"
      direction="rtl"
      :before-close="handleClose">

        <div v-if="type === 'preview'" class="article-preview">
          <h5>{{ form.stem }}</h5>
          <p v-html="form.content"></p>
        </div>

        <el-form v-else ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="标题" prop="stem">
            <el-input v-model="form.content" placeholder="请输入内容"></el-input>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <quill-editor @blur="$refs.form.validateField('content')" v-model="form.stem"></quill-editor>
          </el-form-item>
          <el-form-item align="center" >
            <el-button @click="submit" type="primary">确定</el-button>
            <el-button @click="handleClose">取消</el-button>
          </el-form-item>
        </el-form>
    </el-drawer>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { addArticleList, delArticle, getArticle, getArticleList, updateArticle } from '@/api/article'
export default {
  name: 'article-page',
  components: {
    quillEditor
  },
  data () {
    return {
      form: {
        stem: '',
        content: ''
      },
      rules: {
        stem: [
          { required: true, message: '请输入标题', trigger: ['blur', 'change'] }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: ['blur', 'change'] }
        ]
      },
      current: 1,
      pageSize: 10,
      tableData: [],
      total: 0,
      isShow: false,
      type: 'add'
    }
  },

  created () {
    this.initData()
  },

  methods: {
    handleClose () {
      this.$confirm('确认关闭？')
        .then(_ => {
          // done()
          this.closeDrawer()
        })
        .catch(_ => {})
    },
    async open (type, id) {
      console.log(type, id)
      this.type = type
      this.isShow = true
      if (type !== 'add') {
        const res = await getArticle(id)
        console.log(res)
        this.form = { ...res.data }
      }
    },
    // 获取页面面数
    handleCurrentChange (val) {
      this.current = val
      this.initData()
    },
    // 删除
    async del (id) {
      console.log(id)
      await delArticle(id)
      this.$message.success('删除成功')
      if (this.tableData.length === 1 && this.current > 1) {
        this.current--
      }
      this.initData()
    },
    // async addArticle () {
    //   await addArticleList({
    //     stem: '',
    //     content: ''
    //   })
    // },
    // 封装函数 渲染页面
    async initData () {
      const res = await getArticleList({
        current: this.current,
        pageSize: this.pageSize
      })
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    async submit () {
      // 只能打印出true,因为校验不通过会直接抛出异常
      // 抛出异常没有处理 (try-catch)会终止后面的代码运行
      await this.$refs.form.validate()
      // 发送请求添加
      if (this.type === 'add') {
        await addArticleList(this.form)
        this.$message.success('添加成功')
        this.current = 1
      }
      if (this.type === 'edit') {
        const { id, stem, content } = this.form
        await updateArticle({ id, stem, content })
        this.$message.success('修改成功')
      }
      // 提示用户
      // 添加成功后页面返回第一页
      this.closeDrawer()
      // 重新渲染
      this.initData()
    },
    closeDrawer () {
      // 关闭抽屉
      this.isShow = false
      // 重置表单
      this.$refs.form && this.$refs.form.resetFields()
      this.form = {
        stem: '',
        content: ''
      }
    }
  },
  computed: {
    title () {
      let title = ''
      if (this.type === 'add') title = '添加面经'
      if (this.type === 'preview') title = '面经预览'
      if (this.type === 'edit') title = '修改面经'
      return title
    }
  }
}
</script>

<style lang="scss" scoped>
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
